package com.weikuang.fareast.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.licheedev.myutils.LogPlus;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.weikuang.fareast.R;
import com.weikuang.fareast.activityNew.HomeMainActivity;
import com.weikuang.fareast.bean.LoginHttp;
import com.weikuang.fareast.bean.LoginNewBean;
import com.weikuang.fareast.network.KillerApi;
import com.weikuang.fareast.network.MainApi;
import com.weikuang.fareast.network.MyObserve;
import com.weikuang.fareast.utils.UserHelper;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.utils.PrefUtil;
import cn.dlc.commonlibrary.utils.ToastUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {
    
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;

    String PHONE = "phone";
    String PW = "pw";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        etPhone.setText(PrefUtil.getDefault().getString(PHONE,""));
        etPassword.setText(PrefUtil.getDefault().getString(PW,""));
    }


    private void initData() {
        final String username = etPhone.getText().toString();
        if (TextUtils.isEmpty(username)) {
            ToastUtil.show(this, R.string.please_input_username);
            etPhone.requestFocus();
            return;
        }
        final String password = etPassword.getText().toString();
        if (TextUtils.isEmpty(password)) {
            ToastUtil.show(this, R.string.please_input_password);
            etPassword.requestFocus();
            return;
        }
        showWaitingDialog("请稍等",true);

        LoginHttp loginHttp = new LoginHttp("washApp",password,username,"");
        MainApi.getInstance().getApi( KillerApi.class)
                .login(loginHttp)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new MyObserve<LoginNewBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        dismissWaitingDialog();
                        showOneToast(getString(R.string.order_59));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LoginNewBean bean) {
                        dismissWaitingDialog();
                        UserHelper.get().saveUserInfo(bean);
                        PrefUtil.getDefault().edit().putString(PHONE,username).apply();
                        PrefUtil.getDefault().edit().putString(PW,password).apply();
                        showOneToast(getString(R.string.order_15));
                        startActivity(HomeMainActivity.class);
                    }
                });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @OnClick(R.id.btn_account_login)
    public void onViewClicked() {
        initData();
    }




}

